package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.utils.ConstantValue;
import com.itheima.mobilesafe69.utils.PrefUtils;
import com.itheima.mobilesafe69.utils.ToastUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Setup3Activity extends BaseSetupActivity {
	private static final String tag = "Setup3Activity";
	private Button bt_select_contact;
	private EditText et_phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup3);
		
		initUI();
	}
	
	private void initUI() {
		et_phone = (EditText) findViewById(R.id.et_phone);
		String phone = PrefUtils.getString(this,ConstantValue.CONTACT_PHONE, "");
		et_phone.setText(phone);
		
		bt_select_contact = (Button) findViewById(R.id.bt_select_contact);
		bt_select_contact.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),ContactListActivity.class);
				startActivityForResult(intent, 0);
			}
		});
	}
	
	/* 
	 * 等待联系人界面返回的数据结果
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data!=null){
			String phone = data.getStringExtra("phone");
			phone = phone.replace(" ", "").replace("-", "").trim();
			et_phone.setText(phone);
			
			//将回传的数据做本地sp存储
			PrefUtils.putString(this,ConstantValue.CONTACT_PHONE,phone);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	@Override
	public void showNextPage() {
		String phone = et_phone.getText().toString();
		if(!TextUtils.isEmpty(phone)){
			PrefUtils.putString(this,ConstantValue.CONTACT_PHONE,phone);
			startActivity(new Intent(this, Setup4Activity.class));
			finish();
		}else{
			ToastUtil.show(this, "必须输入电话号码");
		}
		overridePendingTransition(R.anim.next_in_translate, R.anim.next_out_translate);
	}

	@Override
	public void showPrePage() {
		startActivity(new Intent(this, Setup2Activity.class));
		finish();
		
		//上一页移入一出动画
		overridePendingTransition(R.anim.pre_in_translate, R.anim.pre_out_translate);
	}
}
